package com.xyht.medicine_management_system.modules.consumptionPoint.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyht.medicine_management_system.common.commonReturn.CommonResult;
import com.xyht.medicine_management_system.common.commonReturn.ResponseResult;
import com.xyht.medicine_management_system.modules.consumptionPoint.constant.ConsumptionPointRecordConstant;
import com.xyht.medicine_management_system.modules.consumptionPoint.entity.MmsConsumptionPointRecord;
import com.xyht.medicine_management_system.modules.consumptionPoint.entity.MmsUserConsumptionPoint;
import com.xyht.medicine_management_system.modules.consumptionPoint.entity.resp.MmsConsumptionPointRecordResp;
import com.xyht.medicine_management_system.modules.consumptionPoint.entity.resp.MmsUserConsumptionPointResp;
import com.xyht.medicine_management_system.modules.consumptionPoint.mapper.MmsConsumptionPointRecordMapper;
import com.xyht.medicine_management_system.modules.consumptionPoint.mapper.MmsUserConsumptionPointMapper;
import com.xyht.medicine_management_system.modules.consumptionPoint.service.MmsUserConsumptionPointService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.medicine_management_system.common.util.ParamCheckUtil.isNullOrEmpty;

/**
 * <p>
 * 用户消费积分表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-12-11
 */
@Service
public class MmsUserConsumptionPointServiceImpl extends ServiceImpl<MmsUserConsumptionPointMapper, MmsUserConsumptionPoint> implements MmsUserConsumptionPointService {
    @Resource
    private MmsConsumptionPointRecordMapper mmsConsumptionPointRecordMapper;

    @Override
    @Transactional
    public ResponseResult getUserPoint(String userId) {
        //查询个人积分
        List<MmsUserConsumptionPoint> userPointList = this.list(new LambdaQueryWrapper<MmsUserConsumptionPoint>()
                .eq(MmsUserConsumptionPoint::getUserId, userId));
        //若查不到个人积分则创建一条数据并返回初始积分
        if (userPointList.isEmpty()) {
            MmsUserConsumptionPoint userPoint = new MmsUserConsumptionPoint();
            userPoint.setUserId(userId);
            userPoint.setPoints(0);
            this.save(userPoint);
            return CommonResult.success(userPoint);
        }

        MmsUserConsumptionPointResp userPointResp = new MmsUserConsumptionPointResp();
        BeanUtils.copyProperties(userPointList.get(0), userPointResp);
        return CommonResult.success(userPointResp);
    }

    @Override
    @Transactional
    public ResponseResult userPointDetail(Integer pageNum, Integer pageSize, Integer status, String searchStr, String userId) {
        //查询个人积分
        List<MmsUserConsumptionPoint> userPointList = this.list(new LambdaQueryWrapper<MmsUserConsumptionPoint>()
                .eq(MmsUserConsumptionPoint::getUserId, userId));
        //若查不到个人积分则创建一条数据并返回初始积分
        if (userPointList.isEmpty()) {
            MmsUserConsumptionPoint userPoint = new MmsUserConsumptionPoint();
            userPoint.setUserId(userId);
            userPoint.setPoints(0);
            this.save(userPoint);
            return CommonResult.success(userPoint);
        }

        MmsUserConsumptionPointResp userPointResp = new MmsUserConsumptionPointResp();
        BeanUtils.copyProperties(userPointList.get(0), userPointResp);

        //查询积分记录
        LambdaQueryWrapper<MmsConsumptionPointRecord> queryWrapper = new LambdaQueryWrapper<MmsConsumptionPointRecord>()
                .eq(MmsConsumptionPointRecord::getUserId, userId)
                .orderByDesc(MmsConsumptionPointRecord::getCreateTime);
        if (!isNullOrEmpty(searchStr)) {
            StringUtils.deleteWhitespace(searchStr);
            queryWrapper.like(MmsConsumptionPointRecord::getAmount, searchStr).or()
                    .like(MmsConsumptionPointRecord::getPoints, searchStr).or()
                    .like(MmsConsumptionPointRecord::getMedicineInfo, searchStr);
        }

        if (status != null && (status == ConsumptionPointRecordConstant.POINT_RECORD_STATUS_REDUCE || status == ConsumptionPointRecordConstant.POINT_RECORD_STATUS_INCREASE)) {
            queryWrapper.eq(MmsConsumptionPointRecord::getStatus, status);
        }

        Page<MmsConsumptionPointRecord> page = new Page<>(pageNum, pageSize);
        mmsConsumptionPointRecordMapper.selectPage(page, queryWrapper);
        int total = (int) page.getTotal();
        if (total > 0) {
            List<MmsConsumptionPointRecordResp> pointRecordRespList = page.getRecords().stream().map(pointRecord -> {
                MmsConsumptionPointRecordResp pointRecordResp = new MmsConsumptionPointRecordResp();
                BeanUtils.copyProperties(pointRecord, pointRecordResp);
                return pointRecordResp;
            }).collect(Collectors.toList());
            userPointResp.setPointRecordRespList(pointRecordRespList);
        }

        return CommonResult.success(userPointResp, total);
    }
}
